<template>
	<view class="page">
		<view class="risk-form">
			<uni-forms :modelValue="formData" :rules="formRules" ref="formRef" label-position="top" label-width="150">
				<uni-forms-item name="risk_no" label="编号"><uni-easyinput :disabled="!isEditable" type="text" v-model="formData.risk_no" placeholder="自动生成" /></uni-forms-item>
				<uni-forms-item name="risk_code" label="ID">
					<uni-easyinput :disabled="!isEditable" type="text" v-model="formData.risk_code" placeholder="自动生成" />
				</uni-forms-item>
				<uni-forms-item required name="risk_name" label="隐患名称">
					<uni-easyinput :disabled="!isEditable" type="text" v-model="formData.risk_name" placeholder="请输入隐患名称" />
				</uni-forms-item>
				<uni-forms-item required name="risk_level" label="隐患等级">
					<uni-data-select :disabled="!isEditable" v-model="formData.risk_level" :localdata="levelList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item required name="risk_sort" label="隐患类别">
					<uni-data-select :disabled="!isEditable" v-model="formData.risk_sort" :localdata="sortList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item name="risk_reason" label="原因分析">
					<uni-easyinput :disabled="!isEditable" type="textarea" v-model="formData.risk_reason" placeholder="请输入原因分析" />
				</uni-forms-item>
				<uni-forms-item required name="risk_control_measure" label="关联的风险控制措施">
					<uni-easyinput :disabled="!isEditable" type="textarea" v-model="formData.risk_control_measure" placeholder="请输入关联的风险控制措施" />
				</uni-forms-item>
				<uni-forms-item name="risk_result" label="关联的后果">
					<uni-easyinput :disabled="!isEditable" type="textarea" v-model="formData.risk_result" placeholder="请输入关联的后果" />
				</uni-forms-item>
				<uni-forms-item required name="risk_src" label="来源">
					<uni-data-select :disabled="!isEditable" v-model="formData.risk_src" :localdata="srcList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item required name="risk_find_time" label="发现时间">
					<uni-datetime-picker :disabled="!isEditable" type="date" :clear-icon="false" v-model="formData.risk_find_time" />
				</uni-forms-item>
				<uni-forms-item required name="correct_dept" label="整改单位/部门">
					<uni-data-select :disabled="!isEditable" v-model="formData.correct_dept" :localdata="deptList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item required name="correct_time" label="整改时间">
					<uni-datetime-picker :disabled="!isEditable" type="date" :clear-icon="false" v-model="formData.correct_time" />
				</uni-forms-item>
				<uni-forms-item required name="correct_measure" label="整改措施">
					<uni-easyinput :disabled="!isEditable" type="textarea" v-model="formData.correct_measure" placeholder="请输入整改措施" />
				</uni-forms-item>
				<uni-forms-item name="correct_fund" label="整改资金">
					<uni-easyinput :disabled="!isEditable" type="text" v-model="formData.correct_fund" placeholder="请输入整改资金" />
				</uni-forms-item>
				<uni-forms-item name="resp_plan" label="应急预案">
					<uni-easyinput :disabled="!isEditable" type="textarea" v-model="formData.resp_plan" placeholder="请输入应急预案" />
				</uni-forms-item>
				<uni-forms-item required name="measure_verifier" label="措施验证人">
					<uni-easyinput :disabled="!isEditable" type="text" v-model="formData.measure_verifier" placeholder="请输入措施验证人" />
				</uni-forms-item>
				<uni-forms-item required name="measure_verify_time" label="措施验证时间">
					<uni-datetime-picker :disabled="!isEditable" type="date" :clear-icon="false" v-model="formData.measure_verify_time" />
				</uni-forms-item>
				<uni-forms-item required name="manage_effect_verify" label="治理效果验证情况">
					<uni-easyinput :disabled="!isEditable" type="textarea" v-model="formData.manage_effect_verify" placeholder="请输入治理效果验证情况" />
				</uni-forms-item>
				<uni-forms-item required name="is_close" label="是否关闭">
					<uni-data-select :disabled="!isEditable" v-model="formData.is_close" :localdata="closeList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item required name="close_time" label="关闭时间">
					<uni-datetime-picker :disabled="!isEditable || formData.is_close === 0" type="date" :clear-icon="false" v-model="formData.close_time" />
				</uni-forms-item>
			</uni-forms>

			<button v-if="isEditable" @tap="submitRiskForm" class="risk-submit" hover-class="hover-class">提交</button>
		</view>
	</view>
</template>

<script>
import { getCurrentDate } from '@/utils'

export default {
	data() {
		return {
			formType: 'add',
			riskStatus: 0,
			formData: {
				risk_no: '',
				risk_code: '',
				risk_name: '',
				risk_level: '',
				risk_sort: '',
				risk_reason: '',
				risk_control_measure: '',
				risk_result: '',
				risk_src: '',
				risk_find_time: getCurrentDate(),
				correct_dept: '',
				correct_time: getCurrentDate(),
				correct_measure: '',
				correct_fund: '',
				resp_plan: '',
				measure_verifier: '',
				measure_verify_time: getCurrentDate(),
				manage_effect_verify: '',
				is_close: '',
				close_time: getCurrentDate()
			},
			levelList: [],
			sortList: [],
			srcList: [],
			deptList: [],
			closeList: [],
			formRules: {
				risk_name: {
					rules: [
						{
							required: true,
							errorMessage: '请填写隐患名称'
						}
					]
				},
				risk_level: {
					rules: [
						{
							required: true,
							errorMessage: '请选择隐患等级'
						}
					]
				},
				risk_sort: {
					rules: [
						{
							required: true,
							errorMessage: '请选择隐患类别'
						}
					]
				},
				risk_control_measure: {
					rules: [
						{
							required: true,
							errorMessage: '请填写关联的风险控制措施'
						}
					]
				},
				risk_src: {
					rules: [
						{
							required: true,
							errorMessage: '请选择隐患来源'
						}
					]
				},
				risk_find_time: {
					rules: [
						{
							required: true,
							errorMessage: '请选择发现时间'
						}
					]
				},
				correct_dept: {
					rules: [
						{
							required: true,
							errorMessage: '请选择整改单位/部门'
						}
					]
				},
				correct_time: {
					rules: [
						{
							required: true,
							errorMessage: '请选择整改时间'
						}
					]
				},
				correct_measure: {
					rules: [
						{
							required: true,
							errorMessage: '请填写整改措施'
						}
					]
				},
				measure_verifier: {
					rules: [
						{
							required: true,
							errorMessage: '请填写措施验证人'
						}
					]
				},
				measure_verify_time: {
					rules: [
						{
							required: true,
							errorMessage: '请选择措施验证时间'
						}
					]
				},
				manage_effect_verify: {
					rules: [
						{
							required: true,
							errorMessage: '请填写治理效果验证情况'
						}
					]
				},
				is_close: {
					rules: [
						{
							required: true,
							errorMessage: '请选择是否关闭'
						}
					]
				},
				close_time: {
					rules: [
						{
							required: true,
							errorMessage: '请选择关闭时间'
						}
					]
				}
			}
		}
	},
	computed: {
		isEditable() {
			return this.formType === 'add' || this.riskStatus !== 1
		}
	},
	async onLoad(options) {
		console.log('risk form: ', options, this.formData)
		this.formType = options.type
		this.riskStatus = +options.status
		this.formData.risk_code = options.id || 0

		// if (this.formType === 'add') {
		// 	this.formData = Object.assign(this.formData, {
		// 		risk_name: '隐患名称测试01',
		// 		risk_level: 2,
		// 		risk_sort: 2,
		// 		risk_reason: '原因分析测试01',
		// 		risk_control_measure: '关联的风险控制措施测试01',
		// 		risk_result: '关联的后果测试01',
		// 		risk_src: 2,
		// 		risk_find_time: getCurrentDate(),
		// 		correct_dept: 2,
		// 		correct_time: getCurrentDate(),
		// 		correct_measure: '整改措施测试01',
		// 		correct_fund: '10000',
		// 		resp_plan: '应急预案测试01',
		// 		measure_verifier: '张三测试01',
		// 		measure_verify_time: getCurrentDate(),
		// 		manage_effect_verify: '治理效果验证情况测试01',
		// 		is_close: 1,
		// 		close_time: getCurrentDate()
		// 	})
		// }

		await Promise.all([this.getListOneOffice(), this.getRiskDict('level'), this.getRiskDict('category'), this.getRiskDict('source'), this.getRiskDict('yn')])
	},
	onReady() {
		this.$refs.formRef.setRules(this.formRules)

		if (this.formType === 'add') {
			uni.setNavigationBarTitle({
				title: `增加安全隐患`
			})
		} else {
			uni.setNavigationBarTitle({
				title: `编辑安全隐患`
			})
			this.getRiskDetail()
		}
	},
	methods: {
		async getRiskDetail() {
			const params = {
				id: this.formData.risk_code
			}

			try {
				const res = await uni.$uu.Api.getRiskDetail(params)

				console.log(res)

				if (res.code === 100) {
					this.formData = Object.assign({}, this.formData, {
						risk_no: res.content.no,
						risk_name: res.content.name,
						risk_level: res.content.level,
						risk_sort: res.content.category,
						risk_reason: res.content.reason,
						risk_control_measure: res.content.linkMeasure,
						risk_result: res.content.linkResult,
						risk_src: res.content.source,
						risk_find_time: res.content.discoveryTime,
						correct_dept: res.content.changeDept,
						correct_time: res.content.changeTime,
						correct_measure: res.content.changeMeasure,
						correct_fund: res.content.changeAmount,
						resp_plan: res.content.emergencyPlan,
						measure_verifier: res.content.verifyUser,
						measure_verify_time: res.content.verifyTime,
						manage_effect_verify: res.content.verifyResult,
						is_close: res.content.isClose,
						close_time: res.content.closeTime
					})
				}
			} catch (err) {
				console.log(err)
			}
		},
		async getListOneOffice() {
			try {
				const res = await uni.$uu.Api.getListOneOffice()

				console.log(res)

				if (res.code === 100) {
					this.deptList = res.content.map((_) => {
						return {
							text: _.name,
							value: _.id
						}
					})
				}
			} catch (e) {
				console.log(e)
			}
		},
		handleDictData(type, data) {
			switch (type) {
				case 'level':
					this.levelList = data.map((_) => {
						return {
							text: _.name,
							value: +_.value
						}
					})
					break
				case 'category':
					this.sortList = data.map((_) => {
						return {
							text: _.name,
							value: +_.value
						}
					})
					break
				case 'source':
					this.srcList = data.map((_) => {
						return {
							text: _.name,
							value: +_.value
						}
					})
					break
				case 'yn':
					this.closeList = data.map((_) => {
						return {
							text: _.name,
							value: +_.value
						}
					})
					break
				default:
					break
			}
		},
		async getRiskDict(type) {
			const params = {
				type
			}

			try {
				const res = await uni.$uu.Api.getRiskDict(params)
				console.log(res)
				if (res.code === 100) {
					this.handleDictData(type, res.content)
				}
			} catch (e) {
				console.log(e)
			}
		},
		submitRiskForm(e) {
			console.log('submitRiskForm:', this.formData)
			this.$refs.formRef
				.validate()
				.then((res) => {
					console.log('表单数据信息：', res)
					this.editRisk()
				})
				.catch((err) => {
					console.log('表单错误信息：', err)
				})
		},
		async editRisk() {
			let params = {
				name: this.formData.risk_name,
				level: this.formData.risk_level,
				category: this.formData.risk_sort,
				reason: this.formData.risk_reason,
				linkMeasure: this.formData.risk_control_measure,
				linkResult: this.formData.risk_result,
				source: this.formData.risk_src,
				discoveryTime: this.formData.risk_find_time,
				changeDept: this.formData.correct_dept,
				changeTime: this.formData.correct_time,
				changeMeasure: this.formData.risk_name,
				changeAmount: this.formData.correct_fund,
				emergencyPlan: this.formData.resp_plan,
				verifyUser: this.formData.measure_verifier,
				verifyTime: this.formData.measure_verify_time,
				verifyResult: this.formData.manage_effect_verify,
				isClose: this.formData.is_close,
				closeTime: this.formData.close_time
			}

			if (this.formType === 'edit') {
				params['id'] = this.formData.risk_code
			}

			const ApiName = this.formType === 'edit' ? 'editRisk' : 'addRisk'

			try {
				const res = await uni.$uu.Api[ApiName](params)

				console.log(res)

				if (res.code === 100) {
					uni.showToast({
						title: '提交成功',
						success: () => {
							const pages = getCurrentPages()
							if (pages.length > 1) {
								const prevPage = pages[pages.length - 2]
								prevPage.$vm.getData(1)
							}
							uni.navigateBack()
						}
					})
				}
			} catch (err) {
				console.log(err)
			}
		}
	}
}
</script>

<style lang="scss" scoped>
@import '~@/common/app.scss';
@import './index.scss';
</style>
